<template>
    <view class="zw-li" @click="onItemClick(item)">
        <view class="nova-column">
            <view class="nova-row-center">
                <text class="title">{{ getTypeName(item) }}</text>
                <text class="up-tag" v-if="showUpTag()">{{ getUpTag() }}</text>
            </view>
            <view class="nova-row-center" style="padding: 20rpx 0;">
                <image class="loc-icon" src="../../static/zw/zw_loc.png"></image>
                <text class="loc">{{ getVillageName(item) }}</text>
            </view>
            <view class="nova-row-center" style="flex-wrap: wrap;">
                <view class="nova-row-center tag" v-for="v in item.tip" :key="v">
                    <view>{{ v }}</view>
                </view>
            </view>
            <view class="nova-row-center" style="padding-top: 0rpx;">
                <text class="price">{{ item.price || '' }}</text>
                <text class="unit">元/月</text>
                <text style="flex: 1;"></text>
                <text class="time">{{ getPublishTime(item) }}</text>
            </view>
        </view>

        <block v-if="page === 'ay-home'">
            <view class="nova-flex nova-flex-center btn-apply" @click.stop="goApply()">申请</view>
            <block></block>
        </block>
        <block v-if="page === 'ay-apply'">
            <block v-if="detail.job_status == 1">
                <view v-if="detail.job_process == 1" class="nova-flex nova-flex-center btn-apply" @click.stop="cancelApply(detail)">取消申请</view>
                <view v-else-if="detail.job_process > 1" class="nova-flex nova-flex-center btn-apply">{{ getApplyStatus() }}</view>
            </block>
            <view v-else-if="detail.job_status > 1" class="nova-flex nova-flex-center btn-apply">{{ getUpTag() }}</view>
        </block>
        <view v-if="page === 'gs-home'" class="nova-flex nova-flex-center btn-apply" @click.stop="goAccept()">接单</view>
    </view>
</template>

<script>
import conf from '../../common/conf.js';
export default {
    name: 'li-job',
    props: {
        page: {
            //哪个页面
            // 阿姨入口：ay-home, ay-apply
            // 公司入口：gs-home, gs-employ, gs-recommend
            type: String,
            default: ''
        },
        resume_id: {
            type: String,
            default: ''
        },
        item: {
            type: Object,
            default: () => {}
        },
        itemIndex: {
            type: Number,
            default: 0
        }
    },
    data() {
        return {
            role: this.$global.role,
            detail: {}
        };
    },
    created() {
        this.detail = this.item.publishList;
        console.log('from page ' + this.page + ', resume_id ' + this.resume_id);
    },
    watch: {
        item(newVal, oldVal) {
            this.detail = this.item.publishList ? this.item.publishList : {};
        }
    },
    methods: {
        showUpTag() {
            console.log(this.role, this.page);
            // 是否显示名字后面的上标标签
            let show = this.role == 3 && this.page == 'gs-apply';
            return show;
        },
        getUpTag() {
            // 阿姨状态
            return this.$getJobStatus(this.detail.job_status);
        },
        getApplyStatus() {
            return this.$getJobStatus1(this.detail.job_process);
        },
        getTypeName() {
            let type = this.detail.type;
            if (type > 0 && type < conf.ayTypes.length) {
                return conf.ayTypes[type];
            }
            return '其它';
        },
        getVillageName() {
            return this.detail.village_id || this.detail.village;
        },
        getPublishTime() {
            return this.detail.publish_time;
        },

        onItemClick() {
            let id = this.detail.publish_id;
            if (this.$checkIdentification()) {
                uni.navigateTo({
                    url: `../c/p-job-detail?publish_id=${id}&page=${this.page}&resume_id=${this.resume_id}&job_status=${this.detail.job_status}`
                });
            }
        },
        goApply() {
            // ay apply
            if (!this.$checkIdentification(30)) {
                // 填写简历才行
                return;
            }
            let id = this.detail.publish_id;
            uni.setStorage({
                key: 'zw2apply',
                data: this.item,
                success: function(res) {
                    uni.navigateTo({
                        url: '../ay/ay-apply?id=' + id
                    });
                }
            });
        },
        cancelApply() {
            let that = this;
            uni.showModal({
                content: '是否确定取消申请该职位？',
                complete(res) {
                    if (res.confirm) {
                        that.apiCancelApply();
                    }
                }
            });
        },
        async apiCancelApply() {
            let data = {
                publish_id: this.detail.publish_id,
                nurse_json: '',
                remarks: ''
            };
            let res = await this.$request.post({
                url: '/aunt/apply',
                data: data
            });
            this.$toast('取消申请成功');
            let e = {
                index: this.itemIndex,
                publish_id: this.detail.publish_id,
                remove: true,
                page: this.page
            };
            console.log('emit jobStatusChange', e);
            uni.$emit('jobStatusChange', e);
        },
        async goAccept() {
            // gs accept
            if (!this.$checkIdentification(30)) {
                //
                return;
            }
            let res = await this.$request.post({
                url: '/house/ownerChoose',
                data: {
                    publish_id: this.detail.publish_id
                }
            });
            this.$toast('接单成功');
            let e = {
                index: this.itemIndex,
                publish_id: this.detail.publish_id
            };
            if (this.page == 'gs-home') {
                e.remove = true;
            }
            console.log('emit gsAccept', e);
            uni.$emit('jobStatusChange', e);

            let publish_id = this.detail.publish_id;
            setTimeout(function() {
                uni.navigateTo({
                    url: '/pages/gs/gs-recommend-aunty?publish_id=' + publish_id
                });
            }, 500);
        },
        // 推荐职位详情按钮
        recommend_info(item) {
            console.log('推荐职位详情按钮', item);
        }
    }
};
</script>

<style lang="scss">
.zw-li {
    position: relative;
    padding: 34rpx;
    &:after {
        @include divider(34rpx);
    }
    .title {
        font-size: 32rpx;
        color: #333333;
    }
    .loc-icon {
        width: 18rpx;
        height: 22rpx;
        margin-right: 6rpx;
    }
    .loc {
        font-size: 24rpx;
        color: #666666;
    }

    .tag {
        background-color: #f2f2f2;
        border-radius: 6rpx;
        font-size: 24rpx;
        color: #999999;
        height: 36rpx;
        line-height: 36rpx;
        //min-width: 117rpx;
        padding: 0 12rpx;
        margin-right: 24rpx;
        margin-bottom: 20rpx;
        white-space: nowrap;
    }
    .price {
        font-size: 30rpx;
        color: $uni-color-primary;
    }
    .unit {
        font-size: 22rpx;
        color: $uni-color-primary;
    }
    .time {
        font-size: 22rpx;
        color: #999;
    }
    .btn-apply {
        width: 117rpx;
        height: 56rpx;
        position: absolute;
        top: 34rpx;
        right: 34rpx;
        color: $uni-color-primary;
        font-size: 28rpx;
        &:after {
            @include border-1px($uni-color-primary, 6rpx);
            border-radius: 10rpx;
        }
    }
}
</style>
